//初始化执行
$(document).ready(function () {
    initDate()
    // 日历
    $("#data-match").daterangepicker({
        singleDatePicker: false,//设置为单个的datepicker，而不是有区间的datepicker 默认false
        autoUpdateInput: false,  //自动更新input输入框内的值
        autoApply: true,  //不用点击Apply或者应用按钮就可以直接取得选中的日期
        opens: 'right',//设置datepicker面板防止的位置：左边、右边或者中间
        showDropdowns: true,//当设置值为true的时候，允许年份和月份通过下拉框的形式选择 默认false
        timePicker24Hour: true,//设置小时为24小时制 默认false
        timePicker: false,//可选中时分 默认false
        locale: {
            format: "YYYY/MM/DD",  //日期格式
            separator: " - ",
            daysOfWeek: ["日", "一", "二", "三", "四", "五", "六"],
            monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"]
        },
        startDate: moment()
    }, function (start, end, label) {
        if (!this.startDate) {
            this.element.val('').change();
        } else {
            this.element.val(this.startDate.format(this.locale.format)).change();
        }
    });


    $('body').on('click', '.clone-item-js', function (event) {
        var clazz = $(event.target).attr("for");
        var copy = $("." + clazz + "-add").html();
        var container = $(event.target).closest("." + clazz + "-area");
        container.append(copy)
        initDate();
        $("." + clazz).each(function (k, v) {
            $(this).find(".num-js").text(k + 1);
        });
    })

    $('body').on('click', '.del-item-js', function (event) {
        var clazz = $(event.target).attr("for");
        var clazzSelect = "." + clazz;
        swal({
            title: "",
            text: "确定删除吗？",
            type: 'warning',
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            closeOnConfirm: true,
            focusCancel: true // 是否聚焦 取消按钮
        },
        function (isConfirm) {
            if (isConfirm) {
                $(event.target).closest(clazzSelect).remove();
                $("." + clazz).each(function (k, v) {
                    $(this).find(".num-js").text(k + 1);
                });
                return true;
            }
        });



    })

    //新增保存
    $('body').on('click', '.submit_create_btn', function () {
        var title = "是否确认提交";
        swal({
            title: title,
            text: "",
            type: 'warning',
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            closeOnConfirm: false,
            focusCancel: true // 是否聚焦 取消按钮
        },

        function (isConfirm) {
            if (isConfirm) {
                var paramsJson = packageInfo()
                $.ajax({
                    contentType: 'application/json;charset=utf-8', //设置请求头信息
                    url: "/wb/map/graphAttr/create",
                    type: 'POST',
                    data: JSON.stringify(paramsJson),//数据，这里使用的是Json格式进行传输
                    dataType: 'JSON',
                    success: function (data) {
                        layer.closeAll();
                        if (data.status == "200") {
                            parent.layer.alert("保存成功!");
                            setTimeout(function () {
                                window.location.href = "/wb/map/graph/list";
                            }, 500)
                        } else {
                            parent.layer.alert("保存失败!");
                        }
                    }
                });
            }
        });
    })

});

//-------------------------------------------------------自定义函数------------------------------------------------------

function cloneAndLimitElements(clonedContainer, classNames) {
    // 遍历类名数组
    classNames.forEach(function(className) {
        // 查找所有的该类名元素
        var elements = clonedContainer.find('.' + className);

        // 如果找到多个元素，则移除除第一个以外的所有元素
        if (elements.length > 1) {
            for (var i = 1; i < elements.length; i++) {
                elements[i].remove();
            }
        }
    });

    // 返回修改后的克隆容器
    return clonedContainer;
}

//百分比计算 change联动
function initDate() {
    $('.criminalDate-js,.caseDate-js').daterangepicker({
        // autoApply: true,
        autoUpdateInput: false,  //自动更新input输入框内的值
        singleDatePicker: true,
        showDropdowns: true,
        timePicker: false,
        timePicker24Hour: true,//设置小时为24小时制 默认false
        locale: {
            applyLabel: '确定', //确定按钮文本
            cancelLabel: '取消', //取消按钮文本
            format: "YYYY-MM-DD",  //日期格式
            separator: " - ",
            daysOfWeek: ["日", "一", "二", "三", "四", "五", "六"],
            monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"]
        },
    }, function (start, end, label) {
        if (!this.startDate) {
            this.element.val('').change();
        } else {
            this.element.val(this.startDate.format(this.locale.format)).change();
        }
    });
}


function packageInfo() {
    var graphDTO = {};
    graphDTO.id =  $("#graphId").val();
    graphDTO.areaId =  $(".area-js").val();
    graphDTO.areaName =  $(".area-js :selected").text();
    graphDTO.name =  $(".name-js").val();

    var graphClueList = getClueList(graphDTO);
    var graphCaseList = getCaseList(graphDTO);
    var graphUserList = getUserList(graphDTO);

    graphDTO.graphCaseList = graphCaseList;
    graphDTO.graphClueList = graphClueList;
    graphDTO.graphUserList = graphUserList;
    return graphDTO;
}


function getCaseList(graphDTO) {
    var graphCaseList = [];
    $('#mapAttrForm .case-item-js').each(function (index, element) {
        var graphCaseDTO = {};
        var graphUserDetailList = [];

        //基本属性输入框
        $(element).find(".case-attr-js").find('input').each(function () {
            if (!$(this).parents('div.col-sm-3').is(':hidden')) {
                var inpName = $(this).attr('name')
                graphCaseDTO[inpName] = $(this).val()
            }
        })
        //基本属性下拉
        $(element).find(".case-attr-js").find('select').each(function () {
            var inpName = $(this).attr('name')
            graphCaseDTO[inpName] = $(this).val()
        })


        //获取人员
        $(element).find(".case-user-item-js").each(function (index2, element2) {
            var graphUserDetailDTO = {};
            $(element2).find(".case-user-detail-js").find('input').each(function () {
                if (!$(this).parents('div.col-sm-3').is(':hidden')) {
                    var inpName = $(this).attr('name')
                    graphUserDetailDTO[inpName] = $(this).val()
                }
            })
            $(element2).find(".case-user-detail-js").find('select').each(function () {
                var inpName = $(this).attr('name')
                graphUserDetailDTO[inpName] = $(this).val()
            })

            //获取家庭成员属性
            var userDetailFamilyList = [];
            $(element2).find(".case-user-item-family-js").each(function (index3, element3) {
                var userDetailFamilyDTO = {};

                $(element3).find('input').each(function () {
                    if (!$(this).parents('div.col-sm-3').is(':hidden')) {
                        var inpName = $(this).attr('name')
                        userDetailFamilyDTO[inpName] = $(this).val()
                    }
                })
                $(element3).find('select').each(function () {
                    var inpName = $(this).attr('name')
                    userDetailFamilyDTO[inpName] = $(this).val()
                })
                userDetailFamilyList.push(userDetailFamilyDTO);
            });
            graphUserDetailDTO.userDetailFamilyList = userDetailFamilyList;
            graphUserDetailList.push(graphUserDetailDTO);
        });
        graphCaseDTO.graphUserDetailList = graphUserDetailList;

        graphCaseDTO.graphId =  graphDTO.id;
        graphCaseDTO.areaId =  graphDTO.areaId;
        graphCaseDTO.areaName =  graphDTO.areaName;
        graphCaseDTO.graphName =  graphDTO.name;
        graphCaseList.push(graphCaseDTO);
    });
    return graphCaseList;

}

function getClueList(graphDTO) {
    var graphClueList = [];
    $('#mapAttrForm .clue-item-js').each(function (index, element) {
        var graphClueDTO = {};
        var graphUserDetailList = [];
        $(element).find(".base-attr-js").find('input').each(function () {
            if (!$(this).parents('div.col-sm-3').is(':hidden')) {
                var inpName = $(this).attr('name')
                graphClueDTO[inpName] = $(this).val()
            }
        })
        $(element).find(".base-attr-js").find('select').each(function () {
            var inpName = $(this).attr('name')
            graphClueDTO[inpName] = $(this).val()
        })

        $(element).find(".clue-user-item-js").each(function (index2, element2) {
            var graphUserDetailDTO = {};
            $(element2).find('input').each(function () {
                if (!$(this).parents('div.col-sm-3').is(':hidden')) {
                    var inpName = $(this).attr('name')
                    graphUserDetailDTO[inpName] = $(this).val()
                }
            })
            $(element2).find('select').each(function () {
                var inpName = $(this).attr('name')
                graphUserDetailDTO[inpName] = $(this).val()
            })
            graphUserDetailList.push(graphUserDetailDTO);
        });

        graphClueDTO.graphId =  graphDTO.id;
        graphClueDTO.areaId =  graphDTO.areaId;
        graphClueDTO.areaName =  graphDTO.areaName;
        graphClueDTO.graphName =  graphDTO.name;

        graphClueDTO.graphUserDetailList = graphUserDetailList;
        graphClueList.push(graphClueDTO);
    });
    return graphClueList;
}


function getUserList(graphDTO) {
    var graphUserList = [];
    $('#mapAttrForm .user-item-js').each(function (index, element) {
        var graphUserDTO = {};
        $(element).find('input').each(function () {
            if (!$(this).parents('div.col-sm-3').is(':hidden')) {
                var inpName = $(this).attr('name')
                graphUserDTO[inpName] = $(this).val()
            }
        })
        $(element).find('select').each(function () {
            var inpName = $(this).attr('name')
            graphUserDTO[inpName] = $(this).val()
        })

        graphUserDTO.graphId =  graphDTO.id;
        graphUserDTO.areaId =  graphDTO.areaId;
        graphUserDTO.areaName =  graphDTO.areaName;
        graphUserDTO.graphName =  graphDTO.name;
        graphUserList.push(graphUserDTO);
    });
    return graphUserList;
}


function checkMust() {
    var flg = "";
    if (flg != "") {
        flg = flg.replace("*", "");
        flg = flg.replace("：", "");
    }
    return flg;
}

function checkInfo() {
    var flg = "";

    if (flg != "") {
        flg = flg.replace("*", "");
    }
    return flg;

}

